<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Request;
use think\Url;

use app\admin\model\ComValidate;
use app\admin\model\ComModel;

class Cate extends Common{

	//页面目录
	public $dir="./app/index/view/index/";
	public $arr=array();

	public function _initialize(){
		if(request()->isGet()){

			$action= strtolower ( Request::instance()->action() );
			$controller=strtolower( Request::instance()->controller());

			switch ($action) {
				
				case 'catelist':
					$this->assign('title',"分类列表");
					break;
				case 'cateinfo':
					$this->assign('title',"分类信息");
					break;
			}

			$this->assign('controller',$controller);
		}
	}

	#删除分类#
	public function videodel(){

		$videoid=input('videoid');
		echo ComModel::data_del('video_list',['videoid'=>$videoid]);

	}

	#分类分类管理#
	
	#分类列表#
	public function catelist(){
		if(request()->isGet()){
			if(request()->isajax()){

				$page=input('page');
				$limit=input('limit',30);
				$pid=input('pid');

				$map['pid']=empty($pid)?"0":$pid;

				$list=Db::name('cate_list')->where($map)->order('catepx desc')->page($page.','.$limit)->select();
				foreach ($list as $key => $v) {

					$list[$key]['catethumb']=adminthumb($v['catethumb']);

					if($v['status']=='1'){
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
					}else{
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>下架</span>"; 
					}

				}
				$count=Db::name('cate_list')->where($map)->count('cateid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);


			}

			return $this->fetch();
		}
	} 

	//递归获取所有菜单
	public function getlist($list){
		if(is_array($list)){

			foreach ($list as $k => $v) {
				
				$arr=Db::name('cate_list')->where('pid',$v['cateid'])->order('catepx desc')->field('cateid,pid,catename')->select();

				$list[$k]["list"]=$this->getlist($arr);
				
			}

			return $list;

		}

		return $list;
		
	}

	#分类数据#
	public function getcatelist($list,$num="0"){
		
		if(is_array($list)){

			$num++;

			foreach ($list as $k => $v) {

				$arr=array(
					'cateid'=>$v['cateid'],
					'catename'=>str_repeat("&nbsp;&nbsp;&nbsp;",$num)."L_".$v['catename']
				);

				array_push($this->arr,$arr);

				$arr=Db::name('cate_list')->where('pid',$v['cateid'])->order('catepx desc')->field('cateid,pid,catename')->select();

				$list[$k]["list"]=$this->getcatelist($arr,$num);
				
			}

			return $list;

		}

		return $list;
	}	
	#分类分类#
	public function cateinfo(){
		if(request()->isGet()){

			$catelist=Db::name('cate_list')->where('pid','0')->order('catepx desc')->field('cateid,pid,catename')->select();

			$this->getcatelist($catelist);
			
			//整理后的 所有的菜单
			$catelist=$this->arr;

			//目录下文件
			$pages=array("0"=>array(),"1"=>array(),"2"=>array() );

		    foreach (scandir($this->dir) as $k => $v) {
		    	//单页
		       if(preg_match("/page_(.*?).html/",$v,$r)==true){
		            array_push($pages[0], $v);
		        //列表页
		       }else if(preg_match("/list_(.*?).html/",$v,$r)==true){
		       		array_push($pages[1], $v);
		        //内容页
		       }else if(preg_match("/content_(.*?).html/",$v,$r)==true){
		       		array_push($pages[2], $v);
		       }

		    }

			$cateid=input('cateid');
			

			$catetype=Db::name('cate_type')->select();

			if($cateid){

				$this->assign('info',ComModel::data_find('cate_list',['cateid'=>$cateid]));
			}
			
			$this->assign('pages',$pages);
			$this->assign('catetype',$catetype);
			$this->assign('catelist',$catelist);
			
			return $this->fetch();

		}else{

			$data=input('post.');
	
			$data['status']=empty($data['status'])?"0":$data['status'];

			unset($data['file']);

			if($data['cateid']){

				$map=array(
					'cateid'=>array('neq',$data['cateid']),
					'catename'=>$data['catename']
				);
			
				/*if(Db::name('cate_list')->where($map)->count()>0){
					$this->error("当前分类已经存在!");
				}*/

				ComModel::data_up('cate_list',$data,url('catelist'));

			}else{
				
				/*if(Db::name('cate_list')->where(['catename'=>$data['catename']])->count()>0){
					$this->error("当前分类已经存在!");
				}*/

				ComModel::data_add('cate_list',$data,url('catelist'));

			}
		}
	}
	#删除分类#
	public function catedel(){

		$cateid=input('cateid');

		echo ComModel::data_del('cate_list',['cateid'=>$cateid]);
		
	}

	#删除新闻#
	
	public function newsdel(){

		$newsid=input('newsid');

		echo ComModel::data_del('news_list',['newsid'=>$newsid]);
		
	}


	public function content(){
		if(request()->isGet()){
			$cateid=input('cateid');
			$newsid=input('newsid');

			$this->assign('title',"文章详情");
			
			if($newsid){
				$map=array(
					'newsid'=>$newsid,
					'cateid'=>$cateid
				);
			}else{
				$map=array(
					'cateid'=>$cateid
				);
			}
			$catetype=Db::name('cate_list')->where('cateid',$cateid)->value('catetype');

			if($catetype=='文章模型'&& $newsid==''){
				return $this->fetch('content');
			}
			
			//print_r($catetype);exit;
			

			$info=Db::name('news_list')->where($map)->find();
			
			if($info){
				$info['photos']=json_decode($info['photos'],true);
				
				$this->assign('info',$info);
			}
			
			return $this->fetch('content');
			exit;

			switch ($catetype) {

				case '单页面':
					

					$info=Db::name('news_list')->where('cateid',$cateid)->find();
					$this->assign('info',$info);
					return $this->fetch('content');
					
					break;

				case '文章模型':
					
					$this->redirect('newslist',array('cateid'=>$cateid));
					exit;

					$list=Db::name('news_list')->where('cateid',$cateid)->order('addtime desc')->select();
					$this->assign('info',$info);
					return $this->fetch('newslist');
				break;
				default:
					# code...
					break;
			}

		}else{

			$data=input('post.');

			/*print_r($data);exit;
			*/
			
			//转json
			$data['photos']=json_encode($data['photos']);

			unset($data['file']);

			//修改
			if($data['newsid']){

				$r=Db::name('news_list')->update($data);
				
				if($r==true){
					$this->success("修改成功~",url('newslist',array('cateid'=>$data['cateid'])));
				}else{
					$this->error("修改失败~",url('newslist',array('cateid'=>$data['cateid'])));
				}

			//添加
			}else{

				$r=Db::name('news_list')->insert($data);

				if($r==true){
					$this->success("添加成功~",url('newslist',array('cateid'=>$data['cateid'])));
				}else{
					$this->error("添加失败~",url('newslist',array('cateid'=>$data['cateid'])));
				}

			}


		}
	}

	#新闻列表#
	public function newslist(){

		if(request()->isGet()){
			$this->assign('title',"新闻列表");
			return $this->fetch();

		}

	}

	#新闻列表接口#
	public function jsonnewslist(){
		
		$map=[];

			$page=input('page');
			$limit=input('limit');
			$keyword=input('keyword');
			$status=input('status');
			$start=input('start');
			$end=input('end');
			$cateid=input('cateid');

			if($keyword){
				$map['title|keyword']=array("like","%".$keyword."%");
			}
			if($status){
				$map['status']=$status;
			}
			if($start){
				$map['addtime']=array('egt',$start);
			}
			if($end){
				$map['addtime']=array('elt',$end);
			}
			if($start&&$end){
				$map['addtime']=array('between',$start.",".$end);
			}

			if($cateid){
				$map['cateid']=$cateid;
			}

			$list=Db::name('news_list')->where($map)->page($page.','.$limit)->order('newsid desc')->select();
			
			Url::root('/index.php');

			foreach ($list as $key => $v) {
				
				$list[$key]['title']="<a href='".url('index/index/content',array('cateid'=>$v['cateid'],'newsid'=>$v['newsid']))." target='_blank' >".$v['title']."</a>";

				if($v['status']=='1'){
					$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
				}else{
					$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>下架</span>"; 
				}
			}

			$count=Db::name('news_list')->where($map)->count('newsid');

			$arr=array(
				'code'=>"0",
				'count'=>$count,
				'data'=>$list
			);
			return json($arr);
			
	}

	public function newsinfo(){
		
		if(request()->isGet()){

			$newsid=input('newsid');
			$info=Db::name('news_list')->where('newsid',$newsid)->find();

			if($info){
				$this->assign('info',$info);
			}

			return $this->fetch();

		}else{

			$data=input('post.');
			unset($data['file']);
			//修改
			if($data['newsid']){

				$r=Db::name('news_list')->update($data);

				if($r==true){
					$this->success("修改成功~");
				}else{
					$this->error("修改失败~");
				}

			//添加
			}else{

				$r=Db::name('news_list')->insert($data);

				if($r==true){
					$this->success("添加成功~");
				}else{
					$this->error("添加失败~");
				}
			}

		}

	}



}